module textbox(
	input iCLK1,iCLK2,
	input dieKey,
	input guess,
	input rstKey,
	input CLOCK_50,
	input sw1, sw2, sw3, sw4, sw5, sw6, sw7,
	input [9:0] px,
	input [9:0] py,
	input [15:0] guessed,
	output reg valid,
	output reg valid2,
	output reg valid3,
	output pixel,
	output pixel2,
	output pixel3,
	input rst2
);
parameter COLS = 64;
parameter ROWS = 16;
parameter TIMER = 50000000;

reg [26:0] count;
reg [9:0] boxtop;
reg [9:0] boxmiddle;
reg [9:0] boxbottem;
reg [9:0] arm_l;
reg [9:0] arm_r;
reg [9:0] eye_l;
reg [9:0] eye_r;


reg [9:0] ballX;
reg [9:0] ballY;
reg [9:0] ballX2;
reg [9:0] ballY2;
reg [9:0] ballX3;
reg [9:0] ballY3;
reg [9:0] ballX4;
reg [9:0] ballY4;

reg [9:0] ballXv;
reg [9:0] ballYv;
reg [9:0] ballXv2;
reg [9:0] ballYv2;
reg [9:0] ballXv3;
reg [9:0] ballYv3;
reg [9:0] ballXv4;
reg [9:0] ballYv4;

reg [9:0] ballX1;
reg [9:0] ballY1;
reg [9:0] ballX12;
reg [9:0] ballY12;
reg [9:0] ballX13;
reg [9:0] ballY13;
reg [9:0] ballX14;
reg [9:0] ballY14;

reg [9:0] ballXv1;
reg [9:0] ballYv1;
reg [9:0] ballXv12;
reg [9:0] ballYv12;
reg [9:0] ballXv13;
reg [9:0] ballYv13;
reg [9:0] ballXv14;
reg [9:0] ballYv14;

reg [9:0] ballX_2;
reg [9:0] ballY_2;
reg [9:0] ballX22;
reg [9:0] ballY22;
reg [9:0] ballX23;
reg [9:0] ballY23;
reg [9:0] ballX24;
reg [9:0] ballY24;

reg [9:0] ballXv_2;
reg [9:0] ballYv_2;
reg [9:0] ballXv22;
reg [9:0] ballYv22;
reg [9:0] ballXv23;
reg [9:0] ballYv23;
reg [9:0] ballXv24;
reg [9:0] ballYv24;



reg ballXd; // 1=left 0=righ
reg ballYd; // 0=up 1=down

wire [7:0] hangman;

		hang(
			dieKey,
			rstKey,
			guess,
			hangman,
			rst2
		);
		

always@(posedge CLOCK_50 or negedge rstKey)
begin
	if( rstKey ==1'b0)
	begin
		count = 0;
		
//speed of the snow fall in each direction		
		ballXv = 0;
		ballYv = 8;
		ballXv2 = 0;
		ballYv2 = 7;
		ballXv3 = 0;
		ballYv3 = 5;
		ballXv4 = 0;
		ballYv4 = 4;
		
		ballXv1 = 0;
		ballYv1 = 7;
		ballXv12 = 0;
		ballYv12 = 11;
		ballXv13 = 0;
		ballYv13 = 10;
		ballXv14 = 0;
		ballYv14 = 6;
		
		ballXv_2 = 0;
		ballYv_2 = 7;
		ballXv22 = 0;
		ballYv22 = 12;
		ballXv23 = 0;
		ballYv23 = 6;
		ballXv24 = 0;
		ballYv24 = 15;
		
//starting position of snow
		ballX = 80;
		ballY = 400;
		ballX2 = 180;
		ballY2 = 200;
		ballX3 = 260;
		ballY3 = 10;
		ballX4 = 340;
		ballY4 = 0;
		
		ballX1 = 420;
		ballY1 = 0;
		ballX12 = 500;
		ballY12 = 370;
		ballX13 = 580;
		ballY13 = 10;
		ballX14 = 600;
		ballY14 = 0;
		
		ballX_2 = 15;
		ballY_2 = 100;
		ballX22 = 200;
		ballY22 = 620;
		ballX23 = 380;
		ballY23 = 10;
		ballX24 = 400;
		ballY24 = 0;
		
//direction of ball
		ballYd = 1;
		ballXd = 0;
		
	end
	else if (count >= TIMER)
	begin
		count <= 0;

//ball movement
		if (ballYd == 1)
		begin
			ballY <= ballY + ballYv;
			ballY2 <= ballY2 + ballYv2;
			ballY3 <= ballY3 + ballYv3;
			ballY4 <= ballY4 + ballYv4;
			
			ballY1 <= ballY1 + ballYv1;
			ballY12 <= ballY12 + ballYv12;
			ballY13 <= ballY13 + ballYv13;
			ballY14 <= ballY14 + ballYv14;
			
			ballY_2 <= ballY_2 + ballYv_2;
			ballY22 <= ballY22 + ballYv22;
			ballY23 <= ballY23 + ballYv23;
			ballY24 <= ballY24 + ballYv24;
		end
		else
		begin
			ballY <= ballY - ballYv;
			ballY2 <= ballY2 + ballYv2;
			ballY3 <= ballY3 + ballYv3;
			ballY4 <= ballY4 + ballYv4;
			
			ballY1 <= ballY1 - ballYv1;
			ballY12 <= ballY12 + ballYv12;
			ballY13 <= ballY13 + ballYv13;
			ballY14 <= ballY14 + ballYv14;
			
			ballY_2 <= ballY_2 - ballYv_2;
			ballY22 <= ballY22 + ballYv22;
			ballY23 <= ballY23 + ballYv23;
			ballY24 <= ballY24 + ballYv24;
			
		end
		if (ballXd == 1)
		begin
			ballX <= ballX + ballXv;
			ballX2 <= ballX2 + ballXv2;
			ballX3 <= ballX3 + ballXv3;
			ballX4 <= ballX4 + ballXv4;
			
			ballX1 <= ballX1 + ballXv1;
			ballX12 <= ballX12 + ballXv12;
			ballX13 <= ballX13 + ballXv13;
			ballX14 <= ballX14 + ballXv14;
			
			ballX_2 <= ballX_2 + ballXv_2;
			ballX22 <= ballX22 + ballXv22;
			ballX23 <= ballX23 + ballXv23;
			ballX24 <= ballX24 + ballXv24;
		end
		else
		begin
			ballX <= ballX - ballXv;
			ballX2 <= ballX2 - ballXv2;
			ballX3 <= ballX3 - ballXv3;
			ballX4 <= ballX4 - ballXv4;
			
			ballX1 <= ballX1 - ballXv1;
			ballX12 <= ballX12 - ballXv12;
			ballX13 <= ballX13 - ballXv13;
			ballX14 <= ballX14 - ballXv14;
			
			ballX_2 <= ballX_2 - ballXv_2;
			ballX22 <= ballX22 - ballXv22;
			ballX23 <= ballX23 - ballXv23;
			ballX24 <= ballX24 - ballXv24;
		end
		
//update y-position
		if (ballY <= 0 && ballY2 <= 0 && ballY3 <= 0 && ballY4 <= 0 &&ballY1 <= 0
		&& ballY12 <= 0 && ballY13 <= 0 && ballY14 <= 0 && ballY_2 <= 0
		&& ballY22 <= 0 && ballY23 <= 0 && ballY24 <= 0)
			ballYd <= 1;
//update x-position
		if (ballX <= 80 )
			ballXd <= 0;
		else if ( ballX != 80)
			ballX <= 80;
			
		if (ballX2 <= 180 )
			ballXd <= 0;
		else if ( ballX2 != 180)
			ballX2 <= 180;
		
		if (ballX3 <= 260 )
			ballXd <= 0;
		else if ( ballX3 != 260)
			ballX3 <= 260;
			
		if (ballX4 <= 340)
			ballXd <= 0;
		else if ( ballX4 != 340)
			ballX4 <= 340;
			
		if (ballX1 <= 420 )
			ballXd <= 0;
		else if ( ballX1 != 420)
			ballX1 <= 420;
			
		if (ballX12 <= 500 )
			ballXd <= 0;
		else if ( ballX12 != 500)
			ballX12 <= 500;
		
		if (ballX13 <= 580 )
			ballXd <= 0;
		else if ( ballX13 != 580)
			ballX13 <= 580;
			
		if (ballX14 <= 600)
			ballXd <= 0;
		else if ( ballX14 != 600)
			ballX14 <= 600;	
		
		if (ballX_2 <= 420 )
			ballXd <= 0;
		else if ( ballX_2 != 420)
			ballX_2 <= 420;
			
		if (ballX22 <= 500 )
			ballXd <= 0;
		else if ( ballX22 != 500)
			ballX12 <= 500;
		
		if (ballX23 <= 580 )
			ballXd <= 0;
		else if ( ballX23 != 580)
			ballX23 <= 580;
			
		if (ballX24 <= 600)
			ballXd <= 0;
		else if ( ballX24 != 600)
			ballX24 <= 600;
	end
	else
	begin
		count <= count + 10;
	end
end


wire [10:0] address = {qchr[6:0], py[3:0] };
wire [7:0] q;

// calculate character address
wire [9:0] character_address = {py[7:4],px[8:3]};
reg [7:0] qchr;

//create a text box to eliminate snow fall 
always
begin
if (px>=COLS*8) valid3 <= 1'b0;
else if (py>=ROWS*16) valid3 <= 1'b0;
else valid3 <= 1'b1;
end

parameter pressedA = 16'HF01C;
parameter pressedB = 16'HF032;
parameter pressedC = 16'hF021;
parameter pressedD = 16'hF023;
parameter pressedE = 16'hF024;
parameter pressedF = 16'hF02B;
parameter pressedG = 16'hF034;
parameter pressedH = 16'hF033;
parameter pressedI = 16'hF043;
parameter pressedJ = 16'hF03B;
parameter pressedK = 16'hF042;
parameter pressedL = 16'hF04B;
parameter pressedM = 16'hF03A;
parameter pressedN = 16'hF031;
parameter pressedO = 16'hF044;
parameter pressedP = 16'hF04D;
parameter pressedQ = 16'hF015;
parameter pressedR = 16'hF02D;
parameter pressedS = 16'Hf01B;
parameter pressedT = 16'Hf02C;
parameter pressedU = 16'hF03C;
parameter pressedV = 16'hF02A;
parameter pressedW = 16'hF01D;
parameter pressedX = 16'hF022;
parameter pressedY = 16'hF035;
parameter pressedZ = 16'hF01A;
parameter pressedENTER = 16'hF05A;
parameter pressedSPACE = 16'hF029;
parameter pressedBACK	= 16'hF066;

	always@(*)
	begin
			case(character_address)
			 {6'd1,6'd12}:
					begin
					if (guessed == pressedA)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "A";
					end
			 {6'd1,6'd14}:
			 		begin
					if (guessed == pressedB)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "B";
					end
			 {6'd1,6'd16}:
			 		begin
					if (guessed == pressedC)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "C";
					end
			 {6'd1,6'd18}:
			 		begin
					if (guessed == pressedD)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "D";
					end
			 {6'd1,6'd20}:
			 		begin
					if (guessed == pressedE)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "E";
					end
			 {6'd1,6'd22}:
			 		begin
					if (guessed == pressedF)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "F";
					end
			 {6'd1,6'd24}:
			 		begin
					if (guessed == pressedG)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "G";
					end
			 {6'd1,6'd26}:
			 		begin
					if (guessed == pressedH)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "H";
					end
			 {6'd1,6'd28}:
			 		begin
					if (guessed == pressedI)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "I";
					end
			 {6'd1,6'd30}:
					begin
					if (guessed == pressedJ)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "J";
					end
			 {6'd1,6'd32}:
					begin
					if (guessed == pressedK)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "K";
					end
			 {6'd1,6'd34}:
			 		begin
					if (guessed == pressedL)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "L";
					end
			 {6'd1,6'd36}:
					begin
					if (guessed == pressedM)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "M";
					end
			 {6'd1,6'd38}:
			 		begin
					if (guessed == pressedN)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "N";
					end
			 {6'd1,6'd40}:
			 		begin
					if (guessed == pressedO)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "O";
					end
			 {6'd1,6'd42}:
			 		begin
					if (guessed == pressedP)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "P";
					end
			 {6'd1,6'd44}:
			 		begin
					if (guessed == pressedQ)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "Q";
					end
			 {6'd1,6'd46}:
			 		begin
					if (guessed == pressedR)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "R";
					end
			 {6'd1,6'd48}:
			 		begin
					if (guessed == pressedQ)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "S";
					end
			 {6'd1,6'd50}:
					begin
					if (guessed == pressedT)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "T";
					end
			 {6'd1,6'd52}:
					begin
					if (guessed == pressedU)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "U";
					end
			 {6'd1,6'd54}:
					begin
					if (guessed == pressedV)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "V";
					end
			 {6'd1,6'd56}:
					begin
					if (guessed == pressedW)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "W";
					end
			 {6'd1,6'd58}:
					begin
					if (guessed == pressedX)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "X";
					end
			 {6'd1,6'd60}:
					begin
					if (guessed == pressedY)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "Y";
					end
			 {6'd1,6'd62}:
					begin
					if (guessed == pressedZ)
					valid2 <= 1'b0;
					else
					valid2 <= 1'b1;
					qchr <= "Z";
					end
			 default:
					begin
					qchr <= " ";
					valid2 <= 1'b0;
					end
			endcase
			
		end

reg rom_mux_output;

	// Mux to pick off correct rom data bit from 8-bit word
	// for on screen character generation
	always
	case (px[2:0])
	0: rom_mux_output = q[0];
	1: rom_mux_output = q[7];
	2: rom_mux_output = q[6];
	3: rom_mux_output = q[5];
	4: rom_mux_output = q[4];
	5: rom_mux_output = q[3];
	6: rom_mux_output = q[2];
	7: rom_mux_output = q[1];
	endcase
	
	assign pixel2 = rom_mux_output;

// instantiate sysfont memory
sysfont mem2(
	.address(address),
	.clock(iCLK2),
	.q(q)
	);


wire [31:0] px1;
wire [31:0] py1;
always
begin
		if(hangman[0] && px>= boxbottem+240 && px<=boxbottem+400 && py <=boxbottem+400 && py >= boxbottem+280) valid = 1'b1;
		else if (hangman[1] && px>= boxmiddle+260 && px<=boxmiddle+380 && py <=boxmiddle+280 && py >= boxmiddle+200) valid = 1'b1;
		else if (hangman[2] && px>= boxtop+280 && px<=boxtop+360 && py <=boxtop+200 && py >= boxtop+140) valid = 1'b1;
		else if(hangman[3] && px>= arm_r+220 && px<=arm_r + 260 && py <=arm_r+240 && py >= arm_r+235) valid = 1'b1;
		else if (hangman[4] && px>= arm_l+380 && px<=arm_l + 420 && py <=arm_l+240 && py >= arm_l+235) valid = 1'b1;
		else if(px >= ballX && px <= ballX +3 && py>= ballY && py<= ballY +3) valid = 1'b1;
		else if(px >= ballX2 && px <= ballX2 +3 && py>= ballY2 && py<= ballY2 +3) valid = 1'b1;
		else if(px >= ballX3 && px <= ballX3 +3 && py>= ballY3 && py<= ballY3 +3) valid = 1'b1;
		else if(px >= ballX4 && px <= ballX4 +3 && py>= ballY4 && py<= ballY4 +3) valid = 1'b1;
		else if(px >= ballX1 && px <= ballX1 +3 && py>= ballY1 && py<= ballY1 +3) valid = 1'b1;
		else if(px >= ballX12 && px <= ballX12 +3 && py>= ballY12 && py<= ballY12 +3) valid = 1'b1;
		else if(px >= ballX13 && px <= ballX13 +3 && py>= ballY13 && py<= ballY13 +3) valid = 1'b1;
		else if(px >= ballX14 && px <= ballX14 +3 && py>= ballY14 && py<= ballY14 +3) valid = 1'b1;
		else if(px >= ballX_2 && px <= ballX_2 +3 && py>= ballY_2 && py<= ballY_2 +3) valid = 1'b1;
		else if(px >= ballX22 && px <= ballX22 +3 && py>= ballY22 && py<= ballY22 +3) valid = 1'b1;
		else if(px >= ballX23 && px <= ballX23 +3 && py>= ballY23 && py<= ballY23 +3) valid = 1'b1;
		else if(px >= ballX24 && px <= ballX24 +3 && py>= ballY24 && py<= ballY24 +3) valid = 1'b1;
		else valid = 1'b0;


end
assign pixel = 1'b1;
assign pixel3= 1'b1;
assign pixel2 = rom_mux_output;

endmodule



	


	